package cz.muni.fi.pa165.service.local.impl;

import cz.muni.fi.pa165.dao.PackageDAO;
import cz.muni.fi.pa165.dto.PackageDTO;
import cz.muni.fi.pa165.entity.Package;
import cz.muni.fi.pa165.service.local.PackageServiceLocal;
import cz.muni.fi.pa165.shiro.ShiroSecured;
import cz.muni.fi.pa165.transfers.PackageT;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import org.apache.shiro.authz.annotation.RequiresRoles;

/**
 *
 * @author Jakub Janco
 */
@ApplicationScoped
@Named
@ShiroSecured
@RequiresRoles("Manager")
public class PackageService implements PackageServiceLocal {
   
    @EJB
    private PackageDAO packageDAO;

    @Override
    public Long createPackage(PackageDTO pkg) {
        Package entity = PackageT.dTOtoEntity(pkg);
        packageDAO.createPackage(entity);
        return entity.getId();
    }

    @Override
    public void removePackage(PackageDTO pkg) {
        packageDAO.removePackage(PackageT.dTOtoEntity(pkg));
    }

    @Override
    public void updatePackage(PackageDTO pkg) {
        packageDAO.updatePackage(PackageT.dTOtoEntity(pkg));
    }

    @Override
    public PackageDTO getPackageById(Long id) {
        return PackageT.entityToDTO(packageDAO.getPackageById(id));
    }

    @Override
    public List<PackageDTO> getAllPackages() {
        List<Package> packages = packageDAO.getAllPackages();
        List<PackageDTO> packagesDTO = new ArrayList<PackageDTO>();
        for(Package pkg : packages) {
            packagesDTO.add(PackageT.entityToDTO(pkg));
        }
        return packagesDTO;
    }
      
}
